package com.huixh.train.business.service.impl;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson2.JSON;
import com.huixh.train.business.constant.RabbitMqConstant;
import com.huixh.train.business.domain.ConfirmOrder;
import com.huixh.train.business.domain.ConfirmOrderExample;
import com.huixh.train.business.dto.ConfirmOrderMqDto;
import com.huixh.train.business.enums.ConfirmOrderStatusEnum;
import com.huixh.train.business.mapper.ConfirmOrderMapper;
import com.huixh.train.business.req.ConfirmOrderDoReq;
import com.huixh.train.business.req.ConfirmOrderTicketReq;
import com.huixh.train.business.service.SkTokenService;
import com.huixh.train.common.context.LoginMemberContext;
import com.huixh.train.common.exception.BusinessException;
import com.huixh.train.common.exception.BusinessExceptionEnum;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class BeforeConfirmOrderServiceImpl {
    @Resource
    private SkTokenService skTokenService;

    @Resource
    private SnowUtil snowUtil;
    @Resource
    private ConfirmOrderMapper confirmOrderMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    private final static Logger logger = LoggerFactory.getLogger(BeforeConfirmOrderServiceImpl.class);

    public Long beforeConfirmOrder(ConfirmOrderDoReq req) {
        String logId = MDC.get("LOG_ID");
        Long memberId = LoginMemberContext.getMemberId();
        req.setMemberId(memberId);
        req.setLogId(logId);
        // 校验 token
        Boolean verificationed = skTokenService.verificationToken(req.getDate(), req.getTrainCode(), memberId);

        if (!verificationed) {
            throw new BusinessException(BusinessExceptionEnum.CONFIEM_ORDER_SK_TOKEN_FAIL);
        }


        DateTime now = DateTime.now();
        ConfirmOrderExample confirmOrderExample = new ConfirmOrderExample();
        List<ConfirmOrderTicketReq> tickets = req.getTickets();
        confirmOrderExample.createCriteria();

        ConfirmOrder confirmOrder = new ConfirmOrder();
        confirmOrder.setId(snowUtil.getSnowflakeToLong());
        confirmOrder.setMemberId(LoginMemberContext.getMemberId());
        confirmOrder.setDate(req.getDate());
        confirmOrder.setTrainCode(req.getTrainCode());
        confirmOrder.setStart(req.getStart());
        confirmOrder.setEnd(req.getEnd());
        confirmOrder.setDailyTrainTicketId(req.getDailyTrainTicketId());
        confirmOrder.setStatus(ConfirmOrderStatusEnum.INIT.getCode());
        confirmOrder.setCreateTime(now);
        confirmOrder.setUpdateTime(now);
        confirmOrder.setTickets(JSON.toJSONString(tickets));
        confirmOrderMapper.insert(confirmOrder);
        logger.info("订单创建成功：{}", confirmOrder);

        ConfirmOrderMqDto confirmOrderMqDto = new ConfirmOrderMqDto(logId,req.getDate(),req.getTrainCode());
        rabbitTemplate.convertAndSend(RabbitMqConstant.CONFIRM_ORDER, "", confirmOrderMqDto);

        return confirmOrder.getId();
    }
}
